import * as api from '../../service/api'

const state = {
  	productInSale : {},
  	productInWarehouse : {},
  	productList : {},
  	productDetail : {},
  	systemProductList : {},
  	shenPiList : {},
  	productShareAttr : {},
  	productCommission : {},
}

const getters = {
	productInSale : state => state.productInSale,
	productInWarehouse : state => state.productInWarehouse,
	productList : state => state.productList,
	productDetail : state => state.productDetail,
	systemProductList : state => state.systemProductList,
	shenPiList : state => state.shenPiList,
	productShareAttr : state => state.productShareAttr,
	productCommission : state => state.productCommission,
}

const actions = {
	async getProductList ({ commit }, para) {
		let res = await api.productList(
			para.pageNo,
			para.pageSize,
			para.state,
			para.code,
			para.name
		);
		// console.log('state');
		// console.log(para.state);
		// console.log(res.data);
		if(res.errcode === 0){
			if(para.state == '1'){
				commit('setProductInSale', res.data);
			}else if(para.state == '2'){
				commit('setProductInWarehouse', res.data);
			}else{
				commit('setProductList', res.data);
			}
			if(para.cb){
				para.cb(res.data);
			}
		}
	},


	async getProductDetail ({ commit }, para) {
		let res = await api.productDetail(
			para.id
		);
		console.log('getProductDetail res');
		console.log(res);
		if(res.errcode === 0){
			commit('setProductDetail', res.data);
			if(para.cb){
				para.cb(res.data);
			}
		}
	},

	async updateProduct_ticketBase ({ commit }, para) {
		let res = await api.updateProduct_ticketBase(
			para.id, 
			para.sale_code, 
			para.name,
			para.back_type,
			para.sms_type,
			para.sms_diy,
			para.flash_state,
			para.tour_date_type,
			para.take_effect_type,
			para.nopay_cancel_minute,
			para.cardno_state,
			para.isshow,
			para.start_time,
			para.end_time,
			para.tour_date_limit_start,
			para.tour_date_limit_end,
			para.order_sms_state,
			para.order_sms_mobile
		);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async updateProduct_image ({ commit }, para) {
		let res = await api.updateProduct_image(
			para.id, 
			para.sale_code, 
			para.top_pic
		);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async updateProduct_text ({ commit }, para) {
		let res = await api.updateProduct_text(
			para.id, 
			para.sale_code, 
			para.bookingnotes,
			para.detail
		);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async updateProduct_words ({ commit }, para) {
		let res = await api.updateProduct_words(
			para.id, 
			para.sale_code, 
			para.words,
		);
		if(res.errcode === 0){
			para.cb && para.cb(res.data);
			//if(para.cb)  para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async updateProduct_asort ({ commit }, para) {
		let res = await api.updateProduct_asort(
			para.id, 
			para.sale_code, 
			para.asort,
		);
		if(res.errcode === 0){
			para.cb && para.cb(res.data);
			//if(para.cb)  para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async deleteTicket ({ commit }, para) {
		let res = await api.deleteTicket(
			para.id,
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async updateModel ({ commit }, para) {
		let res = await api.updateModel(
			para.attr_id,
			para.sale_code,
			para.attr_name,
			para.cost_price,
			para.guide_price,
			para.stock_type,
			para.current_stock_num,
			para.order_num_state,
			para.order_limit_num,
			para.order_limit_type,
			para.state,
			para.remark,
			para.asort,
			para.sysaffirm_target_goods_supply,
			para.sysaffirm_target_goods_view,
			para.sysaffirm_target_goods_code,
			para.sysaffirm_target_goods_child_flag,
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async updateTicket ({ commit }, para) {
		let res = await api.updateTicket(
			para.id,
			para.code,
			para.name,
			para.periodstart,
			para.periodend,
			para.place_code,
			para.appoint
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async getSystemProductList ({ commit }, para) {
		let res = await api.systemProductList(
			para.pageNo,
			para.pageSize,
			para.isallow,
			para.name
		);
		console.log(res.data);
		if(res.errcode === 0){
			commit('setSystemProductList', res.data);
			if(para.cb){
				para.cb(res.data);
			}
		}
	},


	async shelvesSysProduct ({ commit }, para) {
		let res = await api.shelvesSysProduct(
			para.id,
			para.sale_code
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async getShenPiList ({ commit }, para) {
		let res = await api.shenPiList(
			para.pageNo,
			para.pageSize,
			para.name
		);
		console.log(res.data);
		if(res.errcode === 0){
			commit('setShenPiList', res.data);
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async sysProductAllow ({ commit }, para) {
		let res = await api.sysProductAllow(
			para.code
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async updateSysProduct ({ commit }, para) {
		let res = await api.updateSysProduct(
			para.code,
			para.isallow
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async asortSysProduct ({ commit }, para) {
		let res = await api.asortSysProduct(
			para.asort,
			para.code
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async createProduct_ticket ({ commit }, para) {
		let res = await api.createProduct_ticket(
			para.name,
			para.price_type,
			para.product_attr_sub,
			para.sale_belong,
			para.attr_name,
			para.guide_price,
			para.place_code,
			para.periodstart,
			para.periodend
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async getProductShareAttr ({ commit }, para) {
		let res = await api.getProductShareAttr(
			para.sale_code
		);
		console.log(res.data);
		if(res.errcode === 0){
			commit('setProductShareAttr', res.data);
			if(para.cb){
				para.cb(res.data);
			}
		}else{
			if(para.err){
				para.err(res.errmsg);
			}
		}
	},


	async updateProductShareAttr ({ commit }, para) {
		let res = await api.updateProductShareAttr(
			para.share_pic,
			para.title,
			para.code,
			para.width,
			para.height,
			para.nickname_width,
			para.nickname_height,
			para.nickname_color,
			para.nickname_size,
			para.nickname_length,
			para.qrcode_size
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},

	async getProductCommission ({ commit }, para) {

		let sellerSystemList = await api.sellerSystemList();

		let commissionList = await api.getProductCommission(
			para.product_code
		);

		if(sellerSystemList.errcode != 0 || commissionList.errcode != 0){
			para.err && para.err(res.errmsg);
		}

		// console.log(sellerSystemList.data);
		// console.log(commissionList.data);

		var attrarr = para.attrlist;
		var levelarr = sellerSystemList.data;
		var commissionarr = commissionList.data;

		for(var m = 0; m < attrarr.length; m++){
			var attr = attrarr[m];

			attr['levelarr'] = [];

			for(var i = 0; i < levelarr.length; i++){
				var level = {...levelarr[i]};

				level['profit'] = 0;
				level['profit1'] = 0;
				level['profit2'] = 0;
				level['state'] = 0;

				for(var j = 0; j < commissionarr.length; j++){
					var commission = commissionarr[j];
					if(level.code == commission.level_code
					&& attr.attr_id == commission.attr_id){
						level['profit'] = commission.profit;
						level['profit1'] = commission.profit1;
						level['profit2'] = commission.profit2;												
						level['state'] = commission.state;
						level['id'] = commission.id;
						break;
					}
				}

				attr.levelarr.push(level);
			}
		}

		commit('setProductCommission', attrarr);
		para.cb && para.cb(attrarr);
	},


	async updateProductCommission ({ commit }, para) {
		let res = await api.updateProductCommission(
			para.id,
			para.attr_id, 
			para.level_code, 
			para.code, 
			para.profit, 
			para.profit1, 
			para.profit2,
			para.state
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	async updateProduct_updown ({ commit }, para) {
		let res = await api.updateProduct_updown(
			para.id,
			para.sale_code, 
			para.state
		);
		console.log(res.data);
		if(res.errcode === 0){
			if(para.cb) para.cb(res.data);
		}else{
			if(para.err) para.err(res.errmsg);
		}
	},


	


}

const mutations = {
	setProductInSale (state, obj) {
		state.productInSale = obj;
	},
	setProductInWarehouse (state, obj) {
		state.productInWarehouse = obj;
	},
	setProductList (state, obj) {
		state.productList = obj;
	},
	setProductDetail (state, obj) {
		state.productDetail = obj;
	},
	setSystemProductList (state, obj) {
		state.systemProductList = obj;
	},
	setShenPiList (state, obj) {
		state.shenPiList = obj;
	},
	setProductShareAttr (state, obj) {
		state.productShareAttr = obj;
	},
	setProductCommission (state, obj) {
		state.productCommission = obj;
	},
}

export default {
  state,
  getters,
  actions,
  mutations
}
